package
{
	import com.pesilo.util.UIUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class ScrollBarDispatcher extends EventDispatcher
	{	
		private var ui:Sprite;
		private var track:Sprite;//滑块背景
		private var thumb:Sprite;//滑块
		private var upward:Sprite;//向上按钮
		private var downward:Sprite;//向下按钮
		
		private var _content:Sprite;//内容
		private var _viewport:Sprite;//观察口  （遮罩）
		
		private var ward:Boolean;
		private var dh:Number=0;
		/**
		 * @ui 滚动条视图
		 * @ward 是否具有上下按钮   默认为true，有上下按钮
		 */
		public function ScrollBarDispatcher(ui:Sprite,ward:Boolean=true)
		{
			this.ui=ui;
			this.ward=ward;
			
			track=UIUtil.getElement(ui,"track")as Sprite;
			thumb=UIUtil.getElement(ui,"thumb")as Sprite;
			upward=UIUtil.getElement(ui,"upward")as Sprite;
			downward=UIUtil.getElement(ui,"downward")as Sprite;
			
			if(ward){
				upward.visible=downward.visible=true;
				dh=upward.height;
			}else{
				upward.visible=downward.visible=false;
				dh=0;
			}
		}
		
		public function init(reset:Boolean=false):void{
			//根据ward 注册侦听
			if(!upward.hasEventListener(MouseEvent.CLICK)&&ward){
				upward.addEventListener(MouseEvent.CLICK,__wardClickHandle);
				downward.addEventListener(MouseEvent.CLICK,__wardClickHandle);
			}
			
			if(_viewport.height>=_content.height){
				_content.y=_viewport.y;
				ui.visible=false;
			}else{
				ui.visible=true;
			}
			
			ui.y=_viewport.y;
			upward.y=0;
			downward.y=_viewport.height-downward.height;;
			track.y=dh;
			if(downward.y<upward.y){
				downward.y=upward.y+upward.height;
			}
			//设置滚动条背景的高度
			if(ward){
				track.height=_viewport.height-upward.height-downward.height;
			}else{
				track.height=_viewport.height;
			}
			
			//设置滑块的大小
			thumb.height=_viewport.height/(_content.height)*track.height;
			
			if(reset){
				initialize();
			}else{
				__resizeframe();
			}
			
			
			
			if(!thumb.hasEventListener(MouseEvent.MOUSE_DOWN)){
				thumb.addEventListener(MouseEvent.MOUSE_DOWN,__thumbMouseEventHandle);
				ui.addEventListener(MouseEvent.MOUSE_UP,__thumbMouseEventHandle);
				ui.stage.addEventListener(MouseEvent.MOUSE_UP,__thumbMouseEventHandle);
			}
			
		}
		
		protected function __thumbMouseEventHandle(e:MouseEvent):void
		{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					thumb.startDrag(false,new Rectangle(0,dh,0,track.height-thumb.height));
					
					if(!ui.hasEventListener(Event.ENTER_FRAME)){
						ui.addEventListener(Event.ENTER_FRAME,__enterframe);
					}
					
					break;
				case MouseEvent.MOUSE_UP:
					thumb.stopDrag();
					
					if(ui.hasEventListener(Event.ENTER_FRAME)){
						ui.removeEventListener(Event.ENTER_FRAME,__enterframe);
					}
					break;
			}
		}
		
		/**
		 * 滑块移动时。enterframe事件
		 */
		protected function __enterframe(e:Event=null):void
		{
			_content.y=(thumb.y-dh)*(_content.height-_viewport.height)/(track.height-thumb.height)*-1+ui.y;
			dispatchEvent(new Event("sync"));
		}
		
		/**
		 * 舞台有更变时，调用的函数 
		 */
		protected function __resizeframe():void{
			if(_viewport.height>=_content.height){
				_content.y=_viewport.y;
				ui.visible=false;
				return;
			}
			
			if(_content.y<_viewport.height-_content.height+ui.y){
				_content.y=_viewport.height-_content.height+ui.y;
			}
			thumb.y=(thumb.height/_viewport.height)*(_content.y-_viewport.y)*-1+dh;	
		}
		
		/**
		 * 初始化
		 */
		private function initialize():void{
			thumb.x=track.x;
			if(ward){
				thumb.y=upward.y+upward.height;
			}else{
				thumb.y=track.y;
			}
			_content.y=(thumb.y-dh)*(_content.height-_viewport.height)/(track.height-thumb.height)*-1+ui.y;
		}
		
		/**
		 * 注册向上按钮和向下按钮的侦听事件
		 */
		protected function __wardClickHandle(e:MouseEvent):void
		{
			switch(e.currentTarget){
				case upward:
					trace("upward");
					break;
				case downward:
					trace("downward");
					break;
			}
		}
		
		/**
		 * 为滚动条控制的内容赋值
		 */
		public function set setContent(content:Sprite):void{
			this._content=content;
		}
		
		/**
		 * 为观察口（遮罩）控制的内容赋值
		 */
		public function set setViewport(viewport:Sprite):void{
			this._viewport=viewport;
		}
	}
}